package com.jielin.handling.service.camera.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.amazonaws.services.dynamodbv2.xspec.S;
import com.jielin.handling.db.InquestionsMapper;
import com.jielin.handling.entity.camera.Camera;
import com.jielin.handling.entity.camera.CameraPhoto;
import com.jielin.handling.mapper.camera.CameraMapper;
import com.jielin.handling.service.camera.CameraService;
import com.jielin.handling.util.ConstantUtil;
import com.jielin.handling.util.DateUtil;
import com.jielin.handling.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CameraServiceImpl implements CameraService {

    @Autowired
    private CameraMapper cameraMapper;



    @Override
    public void addCamera(JSONObject camera) {
        //摄像头信息
        JSONArray sxt = camera.getJSONArray("list");
        if(sxt.size()>0){
            for (int i = 0; i < sxt.size(); i++) {
                JSONObject jsonObject = sxt.getJSONObject(i);
                if(!jsonObject.getString("ip").isEmpty()){
                    Camera  camera1 = fzCamera(camera,jsonObject);
                    //新增记录
                    //摄像头ip
                    camera1.setIp(jsonObject.getString("ip"));
                    //摄像头角度
                    camera1.setAngle(jsonObject.getString("angle1"));
                    cameraMapper.addCamera(camera1);
                    //获取此ip下的图片
                    JSONArray tp = jsonObject.getJSONArray("imgUrl");
                    for (int j = 0; j <tp.size() ; j++) {
                        String s = tp.get(j).toString();
                        CameraPhoto cameraPhoto = fzCameraPhoto(camera1, s);
                        //新增记录
                        cameraMapper.addCameraPhoto(cameraPhoto);
                    }
                }
                if(!jsonObject.getString("ip2").isEmpty()){
                    Camera  camera1 = fzCamera(camera,jsonObject);
                    //新增记录
                    //摄像头ip
                    camera1.setIp(jsonObject.getString("ip2"));
                    //摄像头角度
                    camera1.setAngle(jsonObject.getString("angle2"));
                    cameraMapper.addCamera(camera1);
                    //获取此ip下的图片
                    JSONArray tp = jsonObject.getJSONArray("imgUrl2");
                    for (int j = 0; j <tp.size() ; j++) {
                        String s = tp.get(j).toString();
                        CameraPhoto cameraPhoto = fzCameraPhoto(camera1, s);
                        //新增记录
                        cameraMapper.addCameraPhoto(cameraPhoto);
                    }
                }
                if(!jsonObject.getString("ip3").isEmpty()){
                    Camera  camera1 = fzCamera(camera,jsonObject);
                    //新增记录
                    //摄像头ip
                    camera1.setIp(jsonObject.getString("ip3"));
                    //摄像头角度
                    camera1.setAngle(jsonObject.getString("angle3"));
                    cameraMapper.addCamera(camera1);
                    JSONArray tp = jsonObject.getJSONArray("imgUrl3");
                    for (int j = 0; j <tp.size() ; j++) {
                        String s = tp.get(j).toString();
                        CameraPhoto cameraPhoto = fzCameraPhoto(camera1, s);
                        //新增记录
                        cameraMapper.addCameraPhoto(cameraPhoto);
                    }
                }

            }
        }
        //nvr信息
        JSONArray nvr = camera.getJSONArray("lists");
        if(nvr.size()>0){
            for (int i = 0; i < nvr.size(); i++) {
                JSONObject jsonObject = nvr.getJSONObject(i);
                Camera  camera1 = fzCameraNvr(camera,jsonObject);
                //新增记录
                cameraMapper.addCamera(camera1);
            }
        }
    }

    public static boolean GenerateImage(String imgStr, String photoname) {
        //对字节数组字符串进行Base64解码并生成图片
        //图像数据为空
        if (imgStr == null)
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            //Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    //调整异常数据
                    b[i] += 256;
                }
            }
            //生成jpeg图片
            String folderPath = ConstantUtil.PHOTO_PATH+"suspect"+ File.separatorChar+"camera"+File.separatorChar;

            //System.currentTimeMillis()
            //新生成的图片
            String imgFilePath = folderPath + photoname;
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public CameraPhoto fzCameraPhoto(Camera camera, String jsonObject){
        CameraPhoto  camera1 = new CameraPhoto();
        //id
        camera1.setId(StringUtil.generatorUUID());
        //摄像头id
        camera1.setCameraId(camera.getId());
        //市县
        camera1.setCity(camera.getCity());
        //派出所
        camera1.setPoliceStation(camera.getPoliceStation());
        //上报人
        camera1.setReportPeople(camera.getReportPeople());
        //联系电话
        camera1.setPhone(camera.getPhone());
        //图片
        camera1.setPhoto(jsonObject);
        return camera1;
    }

    public Camera fzCamera(JSONObject camera,JSONObject jsonObject){
            Camera  camera1 = new Camera();
            //摄像头id
            camera1.setId(StringUtil.generatorUUID());
            //市县
            camera1.setCity(camera.getString("city"));
            //派出所
            camera1.setPoliceStation(camera.getString("policeStation"));
            //上报人
            camera1.setReportPeople(camera.getString("reportPeople"));
            //联系电话
            camera1.setPhone(camera.getString("phone"));


        //摄像头用户名
        camera1.setUsername(jsonObject.getString("username"));
        //摄像头用户密码
        camera1.setPassword(jsonObject.getString("password"));
        //摄像头型号
        camera1.setModel(jsonObject.getString("model"));
        //摄像头品牌
        camera1.setBrand(jsonObject.getString("brand"));
        //摄像头位置
        camera1.setLocation(jsonObject.getString("location"));

        return camera1;
    }

    public Camera fzCameraNvr(JSONObject camera,JSONObject jsonObject){
        Camera  camera1 = new Camera();
        //摄像头id
        camera1.setId(StringUtil.generatorUUID());
        //市县
        camera1.setCity(camera.getString("city"));
        //派出所
        camera1.setPoliceStation(camera.getString("policeStation"));
        //上报人
        camera1.setReportPeople(camera.getString("reportPeople"));
        //联系电话
        camera1.setPhone(camera.getString("phone"));

        //摄像头用户名
        camera1.setUsername(jsonObject.getString("nvrusername"));
        //摄像头用户密码
        camera1.setPassword(jsonObject.getString("nvrpassword"));
        //摄像头ip
        camera1.setIp(jsonObject.getString("nvrips"));
        //摄像头型号
        camera1.setModel(jsonObject.getString("idmodel"));
        //摄像头品牌
        camera1.setBrand(jsonObject.getString("nvrbrand"));
        //摄像头角度
        camera1.setAngle(jsonObject.getString("nvrangle"));
        //摄像头位置
        camera1.setLocation("nvr");
        return camera1;
    }



    @Override
    public int updateCamera(Camera camera) {
        int i =cameraMapper.updateCamera(camera);
        return i;
    }

    @Override
    public List<Map<String, Object>> selectCity() {
        List<Map<String, Object>> map =cameraMapper.selectCity();
        return map;
    }

    @Override
    public List<Map<String, Object>> selectPoliceStation(JSONObject jsonObject) {
        List<Map<String, Object>> map =cameraMapper.selectPoliceStation(jsonObject);
        return map;
    }

    @Override
    public List<Camera> selectCamera(Camera camera) {


        return  cameraMapper.selectCamera(camera);
    }
}
